1 // Copyright (C) 2024 Rubén Beltrán del Río
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see https://map.tranquil.systems.
18 struct MapGroup: View {
21 let vertexSize: CGSize
25 let cornerSize = CGSize(width: 2.0, height: 2.0)
26 var strokeSize: CGFloat { 1.75 * vertexSize.width }
29 let groupList = group.map({ vertex in
30 return [Double(vertex.position.x), Double(vertex.position.y)]
33 let hullPoints = hull.hull(groupList, nil)
34 return hullPoints.compactMap({ object in
35 if let point = object as? [Double] {
36 return CGPoint(x: point[0], y: point[1])
44 var initialMove: CGPoint?
47 let offsetPoint = CGPoint(x: w(point.x), y: h(point.y))
49 if initialMove == nil {
50 path.move(to: offsetPoint)
51 initialMove = offsetPoint
53 path.addLine(to: offsetPoint)
57 if let initialMove = initialMove {
58 path.addLine(to: initialMove)
63 CGAffineTransform(translationX: vertexSize.width / 2.0, y: vertexSize.height / 2.0)
69 lineWidth: strokeSize,
79 func h(_ dimension: CGFloat) -> CGFloat {
80 max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
83 func w(_ dimension: CGFloat) -> CGFloat {
84 max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
90 mapSize: CGSize(width: 400.0, height: 400.0), vertexSize: CGSize(width: 25.0, height: 25.0),
92 Vertex(id: 0, label: "A Circle", position: CGPoint(x: 50.0, y: 50.0)),
93 Vertex(id: 1, label: "A Square", position: CGPoint(x: 10.0, y: 20.0), shape: .square),
94 Vertex(id: 2, label: "A triangle", position: CGPoint(x: 25, y: 32.0), shape: .triangle),
95 Vertex(id: 3, label: "An X", position: CGPoint(x: 70.0, y: 70.0), shape: .x),